package com.atguigu.gmall.activity.consumer;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.activity.mapper.SeckillGoodsMapper;
import com.atguigu.gmall.activity.pojo.SeckillOrder;
import com.atguigu.gmall.activity.pojo.UserRecode;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.activity.SeckillGoods;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @author luYinLei
 * @version 1.0
 * @date 2021/7/16 19:05
 */
@Component
public class SeckillOrderAddConsumer {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private RabbitService rabbitService;

    @RabbitListener(queues = "seckill_queue")
    public void addOrder(String smg) {
        //转换数据类型
        UserRecode userRecode = JSONObject.parseObject(smg, UserRecode.class);
        //获取下单用户id
        String username = userRecode.getUsername();
        //获取下单时间
        String time = userRecode.getTime();
        //获取商品id
        String goodsId = userRecode.getGoodsId();
        //校验商品
        SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps("SeckillGoods_" + time)
                                                        .get(goodsId);
        if (seckillGoods!=null){
            Object o = redisTemplate.boundListOps("seckillGoodsQueue_" + goodsId).rightPop();
            //判断库存
            if (o != null) {
                //构建订单信息
                SeckillOrder seckillOrder = new SeckillOrder();
                seckillOrder.setId(UUID.randomUUID().toString().replace("-", ""));
                seckillOrder.setGoodsId(goodsId);
                seckillOrder.setNum(userRecode.getNum());
                seckillOrder.setMoney(userRecode.getNum() * seckillGoods.getCostPrice().doubleValue() + "");
                seckillOrder.setStatus("0");
                seckillOrder.setUserId(username);
                seckillOrder.setCreateTime(userRecode.getCreateTime());
                //将订单信息存入redis;
                redisTemplate.boundHashOps("SeckillOrder_" + username).put(seckillOrder.getId(), seckillOrder);
                //跟新库存。防止超卖
                Long stockNum = redisTemplate.boundHashOps("SeckillOrderStockNum").increment(goodsId, -1);
                //修改秒杀商品库存
                seckillGoods.setStockCount(stockNum.intValue());
                //判断
                if (stockNum>0){
                    //存入redis
                    redisTemplate.boundHashOps("SeckillGoods_" + time).put(goodsId,seckillGoods);
                }else {
                    //商品售罄，删除redis中商品数据
                    redisTemplate.boundHashOps("SeckillGoods_" + time).delete(goodsId);
                    //存入数据库
                    seckillGoodsMapper.updateById(seckillGoods);
                }
                //更新排队信息
                userRecode.setStatus(2);
                userRecode.setOrderId(seckillOrder.getId());
                userRecode.setMoney(Float.valueOf(seckillOrder.getMoney()));
                //更新redis排队状态
                redisTemplate.boundValueOps("SeckillStatus_" +username).set(userRecode);
                //发送延迟队列消息，记录订单的有效倒计时
//                rabbitService.sendMessage(
//                        "seckill_dead_exchange",
//                        "seckill.dead.order",
//                        JSONObject.toJSONString(seckillOrder),
//                        "300");
                // 结束
                return;
            }
        }
        //商品售罄，修改排队状态,下单失败
        userRecode.setStatus(3);
        //更新redis
        redisTemplate.boundValueOps("SeckillStatus_" + username).set(userRecode);
        //删除redis商品数据
        redisTemplate.delete("SeckillUserQueue_"+username);
        return;
    }
}
